<script lang="ts">
export default {
  name: 'manage',
};
</script>

<script lang="ts" setup>

import {onMounted, reactive, ref} from 'vue';
import {ElMessage, ElMessageBox} from 'element-plus';

import RepoCompEntityTable from '../components/RepoCompEntityTable.vue';
import RepoCompEntityHeader from './components/RepoCompEntityHeader.vue';
import RepoCompEntityDialog from '../components/RepoCompEntityDialog.vue';
import RepoCompVersionDialog from '../components/RepoCompVersionDialog.vue';


// 引入设备的API和数据类型
import {
  CreateRepoCompRequestVO,
  RepoCompQueryParam,
} from '@/api/cloud/repository/repocomp/file/types';
import {
  createRepoCompEntity,
  updateRepoCompEntity,
  deleteRepoCompEntity,
  deleteRepoCompVerEntity,
  updateRepoCompVerEntity
} from "@/api/cloud/repository/repocomp/file";


const repoCompEntityTableRef = ref<any>(); // 这了的变量名称必须和<console-edit-dialog>的ref值一样
const repoCompEntityHeaderRef = ref<any>(); // 这了的变量名称必须和<console-edit-dialog>的ref值一样
const repoCompEntityDialogRef = ref<any>(); // 这了的变量名称必须和<console-edit-dialog>的ref值一样
const repoCompVersionDialogRef = ref<any>(); // 这了的变量名称必须和<console-edit-dialog>的ref值一样

const dataState = reactive({
  // 查询参数
  queryParams: {pageNum: 1, pageSize: 10} as RepoCompQueryParam,
  // 对话框状态
  editDialog: {
    visible: false,
  },
  // 对话框状态
  versionDialog: {
    visible: false,
  },
});

function handleCreateDialog(source: string, type: any) {
  // 创建对话框
  if (source === 'header' && type === 'create') {
    dataState.editDialog.visible = true;
    repoCompEntityDialogRef.value.initEditData(type, undefined);
    return;
  }
}

/**
 * 捕获来自对话框的创建操作
 * @taskParam value
 */
async function handleCreate(value: any) {
  let formData = {
    groupId: value.groupId,
    groupName: value.groupName,
    modelType: value.modelType,
    modelName: value.modelName,
    modelVersion: value.modelVersion,
    component: value.component,
    description: value.description,
  }

  await createRepoCompEntity(formData).then(() => {
    ElMessage.success('新增成功');
    // 隐藏对话框
    dataState.editDialog.visible = false;
    // 刷新表单
    repoCompEntityTableRef.value.refreshTable(dataState.queryParams);
  })
}

async function handleUpdate(value: any) {
  let formData = {
    id: value.id,
    groupId: value.groupId,
    groupName: value.groupName,
    modelType: value.modelType,
    modelName: value.modelName,
    modelVersion: value.modelVersion,
    component: value.component,
    description: value.description,
  }

  await updateRepoCompEntity(formData).then(() => {
    ElMessage.success('更新成功');
    // 隐藏对话框
    dataState.editDialog.visible = false;
    // 刷新表单
    repoCompEntityTableRef.value.refreshTable(dataState.queryParams);
  })
}

function handleUpdateDialog(source: string, type: any, value: any) {
  if (source === 'table' && type === 'update') {
    if (value === undefined) {
      return;
    }

    // 初始化编辑对话框的数据
    repoCompEntityDialogRef.value.initEditData(type, value);
    // 显示对话框
    dataState.editDialog.visible = true;
  }
}

function handleDelete(source: any) {
  let data = {} as CreateRepoCompRequestVO;
  data.modelName = source.modelName;
  data.modelType = source.modelType;
  data.modelVersion = source.modelVersion;

  ElMessageBox.confirm('确认删除?', '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(() => {
    deleteRepoCompEntity(data).then(() => {
      ElMessage.success('删除成功');
      // 刷新表单
      repoCompEntityTableRef.value.refreshTable(dataState.queryParams);
    });
  })
      .catch(() => ElMessage.info('已取消删除'));

}

function handleDeleteVersion(source: any) {
  let data = {} as CreateRepoCompRequestVO;
  data.modelName = source.modelName;
  data.modelType = source.modelType;
  data.modelVersion = source.modelVersion;
  data.version = source.version;
  data.stage = source.stage;

  ElMessageBox.confirm('确认删除?', '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(() => {
    deleteRepoCompVerEntity(data).then(() => {
      ElMessage.success('删除成功');
      // 刷新表单
      repoCompEntityTableRef.value.refreshTable(dataState.queryParams);
    });
  })
      .catch(() => ElMessage.info('已取消删除'));

}

async function handleUpdateVersion(source: any) {
  // 创建对话框
  dataState.versionDialog.visible = true;
  repoCompVersionDialogRef.value.initEditData('update', source);
}

/**
 * 捕获对话框的取消操作
 * @taskParam value
 */
function handleVersionCancel() {
  // 隐藏对话框
  dataState.versionDialog.visible = false;
}

/**
 * 捕获来自对话框的创建操作
 * @taskParam value
 */
async function handleVersionUpdate(value: any) {
  let formData = {
    modelName: value.verItem.modelName,
    modelType: value.verItem.modelType,
    modelVersion: value.verItem.modelVersion,
    version: value.verItem.version,
    stage: value.verItem.stage,

    newStage: value.stage,
  }

  if (formData.newStage === undefined) {
    return;
  }
  if (formData.newStage === '') {
    return;
  }

  await updateRepoCompVerEntity(formData).then(() => {
    ElMessage.success('更新成功');
    // 隐藏对话框
    dataState.versionDialog.visible = false;
    // 刷新表单
    repoCompEntityTableRef.value.refreshTable(dataState.queryParams);
  })
}

/**
 * 捕获对话框的取消操作
 * @taskParam value
 */
function handleCancel() {
  // 隐藏对话框
  dataState.editDialog.visible = false;
}

/**
 * 响应查询按钮：页查询数据
 */
function handleQuery(source: string, value: any) {
  dataState.queryParams = value;
  dataState.queryParams.modelType = "system";
  repoCompEntityTableRef.value.refreshTable(dataState.queryParams);
}

/**
 * 响应页面安装：页面的初始化工作
 */
onMounted(() => { 
});
</script>

<template>
  <div class="app-container">

    <!-- 表单头：各种按钮 -->
    <repo-comp-entity-header ref="repoCompEntityHeaderRef"
                             @create="handleCreateDialog"
                             @query="handleQuery"
    />

    <!-- 数据表单：创建/修改 -->
    <repo-comp-entity-table ref="repoCompEntityTableRef"
                            :modelType="'system'"
                            @delete="handleDelete"
                            @deleteVersion="handleDeleteVersion"
                            @update="handleUpdateDialog"
                            @updateVersion="handleUpdateVersion"
    />

    <!-- 设备编辑对话框：创建/修改 -->
    <repo-comp-entity-dialog ref="repoCompEntityDialogRef" v-model:visible="dataState.editDialog.visible"
                             @cancel="handleCancel"
                             @create="handleCreate"
                             @update="handleUpdate"
    />

    <!-- 设备编辑对话框：创建/修改 -->
    <repo-comp-version-dialog ref="repoCompVersionDialogRef" v-model:visible="dataState.versionDialog.visible"
                              @cancel="handleVersionCancel"
                              @update="handleVersionUpdate"
    />
  </div>
</template>
